/**
 * 分类管理
 */

<template>
  <div class="contenBox">
    <!-- 左侧导航 -->
    <div class="leftTree">
      <div class="leftTreeBox">
        <el-tree
          :data="classifyTreeList"
          :highlight-current="true"
          @current-change="setParentId"
        ></el-tree>
      </div>
    </div>

    <!-- 右侧内容 -->
    <div class="rightContent">
      <div class="rightTop">
        <div class="rightTitle">
          <span>管理列表 </span>
        </div>
        <div class="rightSearch">
          <div class="rightSearchInput">
            <el-input
              v-model="searchVal"
              placeholder="请输入内容"
              suffix-icon="el-icon-search"
              @input="searchInput"
            ></el-input>
          </div>

          <el-button type="primary" @click="openAddClassify">
            <i class="el-icon-plus"></i>
            添加分类
          </el-button>

          <el-button type="danger" @click="batchDelClassify">
            <i class="el-icon-close"></i>
            批量删除
          </el-button>

          <el-button class="resetBtn" @click="refreshInput">
            <i class="el-icon-refresh"></i>
            <span>重置</span>
          </el-button>
        </div>
      </div>

      <div class="rightBottom">
        <!-- 表格 -->
        <el-table
          ref="multipleTable"
          :data="classifyList"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50"></el-table-column>
          <el-table-column
            label="序号"
            type="index"
            width="50"
          ></el-table-column>
          <el-table-column prop="typeName" label="类型名称"></el-table-column>
          <el-table-column prop="parentTypeName" label="上级类型组">
            <template slot-scope="scope">
              {{ scope.row.parentTypeName ? scope.row.parentTypeName : "无" }}
            </template>
          </el-table-column>

          <el-table-column prop="typeLabel" label="类型标识"></el-table-column>
          <el-table-column label="启用状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#C2C2C2"
                @change="updateIsUsing(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="action" label="操作">
            <template slot-scope="scope">
              <span
                class="bianji"
                @click="openEditClassify(scope.row)"
              >编辑</span>
              <span
                class="del"
                @click="delClassify(scope.row.typeId)"
              >删除</span>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination" v-if="classifyList.length > 0">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="10"
            prev-text="上一页"
            next-text="下一页"
            layout=" prev, pager, next"
            :total="total"
          >
          </el-pagination>
          <div class="jumper">
            跳到第
            <input type="text" ref="currentPage" value="1" />
            页
          </div>
          <button class="submit" @click="goPage">确定</button>
        </div>
      </div>
    </div>

    <div class="dialog">
      <!-- 添加分类 -->
      <el-dialog
        :title="isEidt ? '编辑类型' : '添加类型'"
        :visible.sync="dialogFormVisible"
        center
      >
        <el-form :model="form">
          <el-form-item label="类型名称:" :label-width="formLabelWidth">
            <el-input
              v-model="form.typeName"
              autocomplete="off"
              placeholder="请输入类型名称"
              style="width: 280px"
            ></el-input>
          </el-form-item>

          <el-form-item label="上级类型:" :label-width="formLabelWidth">
            <span v-if="form.level == '1'">无</span>
            <el-select
              v-model="form.parentId"
              placeholder="请选择上级类型"
              style="width: 280px"
              v-else
            >
              <el-option
                :label="item.label"
                :value="item.id"
                v-for="(item, index) in classifyTreeList"
                :key="index"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="等级:" :label-width="formLabelWidth">
            <el-input
              v-model="form.level"
              autocomplete="off"
              placeholder="请输入等级"
              style="width: 280px"
            ></el-input>
          </el-form-item>

          <el-form-item label="类型标识:" :label-width="formLabelWidth">
            <el-input
              v-model="form.typeLabel"
              autocomplete="off"
              placeholder="请输入类型标识"
              style="width: 280px"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAddClassify">取 消</el-button>
          <el-button type="primary" @click="confirmAddClassify"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  addClassifyListApi,
  delClassifyListApi,
  editClassifyListApi,
  getClassifyListApi,
  getClassifyTreeListApi,
} from "@/api/basics/classify";
export default {
  data() {
    return {
      total: 0,
      classifyList: [],
      classifyTreeList: [],
      isEidt: false,
      searchVal: "",
      multipleSelection: [],
      currentPage: 1,
      pageSize: 10,
      dialogFormVisible: false,
      form: {
        typeId: "",
        typeName: "",
        parentId: "",
        level: "",
        typeLabel: "",
      },
      formLabelWidth: "100px",
      parentId: "",
    };
  },

  mounted() {
    // 获取 左侧树 列表
    this.getClassifyTreeList();
  },

  methods: {
    // 获取 左侧树 列表
    getClassifyTreeList() {
      getClassifyTreeListApi().then((res) => {
        if (res.code === 200) {
          this.classifyTreeList = res.data;

          // 获取分类列表
          this.getClassifyList({
            pageNum: this.currentPage,
            pageSize: this.pageSize,
          });
        }
      });
    },

    // 设置 parentId
    setParentId(data) {
      this.parentId = data.id;
      // 获取分类列表
      this.getClassifyList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        parentId: this.parentId,
      });
    },

    // 获取分类列表
    getClassifyList(data) {
      getClassifyListApi({ parentId: this.parentId, ...data }).then((res) => {
        if (res.code === 200) {
          this.total = res.total;
          this.classifyList = res.rows.map((item) => {
            return {
              ...item,
              status: item.status == "0" ? true : false,
            };
          });
        }
      });
    },

    // 搜索
    searchInput() {
      if (this.searchVal) {
        this.getClassifyList({
          typeName: this.searchVal.trim(),
          pageNum: this.currentPage,
          pageSize: this.pageSize,
        });
      } else {
        this.getClassifyList({
          pageNum: this.currentPage,
          pageSize: this.pageSize,
        });
      }
    },

    // 重置
    refreshInput() {
      this.searchVal = "";
      this.parentId = "";
      this.getClassifyList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      });
    },

    // 添加分类
    openAddClassify() {
      this.form = {
        typeId: "",
        typeName: "",
        parentId: "",
        level: "",
        typeLabel: "",
      };
      this.isEidt = false;
      this.dialogFormVisible = true;
    },

    // 编辑分类
    openEditClassify(data) {
      console.log(data);
      let { typeId, typeName, parentId, level, typeLabel } = data;
      this.form = {
        typeId,
        typeName,
        parentId,
        level,
        typeLabel,
      };
      this.isEidt = true;
      this.dialogFormVisible = true;
    },

    // 取消新增
    closeAddClassify() {
      this.dialogFormVisible = false;
    },

    // 确定新增 / 编辑
    confirmAddClassify() {
      if (this.form.typeName) {
        this.dialogFormVisible = false;
        if (this.isEidt) {
          editClassifyListApi(this.form).then((res) => {
            if (res.code == 200) {
              this.$message.success("修改成功！");
              this.getClassifyList({
                pageNum: this.currentPage,
                pageSize: this.pageSize,
              });
              this.getClassifyTreeList();
            } else {
              this.$message.error("修改失败！");
            }
          });
        } else {
          addClassifyListApi({
            ...this.form,
            status: "0",
          }).then((res) => {
            if (res.code == 200) {
              this.$message.success("添加成功！");
              this.getClassifyList({
                pageNum: this.currentPage,
                pageSize: this.pageSize,
              });
              this.getClassifyTreeList();
            } else {
              this.$message.error("添加失败！");
            }
          });
        }
      } else {
        this.$message.error("类型名称不能为空！");
      }
    },

    // 删除
    delClassify(ids) {
      this.$confirm("此操作将删除该类型, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delClassifyListApi({ ids }).then((res) => {
          if (res.code == 200) {
            this.$message.success("删除成功！");
            this.getClassifyList({
              pageNum: this.currentPage,
              pageSize: this.pageSize,
            });
            this.getClassifyTreeList();
          } else {
            this.$message.error("删除失败！");
          }
        });
      });
    },

    // 批量删除
    batchDelClassify() {
      if (this.multipleSelection.length) {
        let data = [...this.multipleSelection].map((item) => {
          return "" + item.typeId;
        });

        this.$confirm("此操作将删除该类型, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          delClassifyListApi({ ids: data.join(",") }).then(() => {
            this.$message.success("删除成功！");
            this.getClassifyList({
              pageNum: this.currentPage,
              pageSize: this.pageSize,
            });
            this.getClassifyTreeList();
          });
        });
      } else {
        this.$message.error("请选择要删除的分类！");
      }
    },

    // 修改启用状态
    updateIsUsing(data) {
      let { typeId, status } = data;
      editClassifyListApi({
        typeId,
        status: status ? "0" : "1",
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("修改成功！");
          this.dialogFormVisible = false;
          this.getClassifyList({
            pageNum: this.currentPage,
            pageSize: this.pageSize,
          });
        } else {
          this.$message.error("修改失败！");
        }
      });
    },

    // 已选中
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 设置当前页码
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getClassifyList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      });
    },

    // 跳转指定页码
    goPage() {
      let { value } = this.$refs.currentPage;
      this.currentPage = Number(value);
      this.getClassifyList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.contenBox {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: space-between;
  background-color: #f3f2f7;
  padding: 0 15px 15px;
  box-sizing: border-box;

  .leftTree {
    min-width: 250px;
    height: 100%;
    background-color: #fff;
    border-radius: 5px;
    box-sizing: border-box;
    overflow-y: scroll;
    overflow-x: hidden;

    .leftTreeBox {
      width: 100%;
      height: calc(100vh - 110px);
    }

    ::v-deep .el-tree {
      width: 100%;
    }

    ::v-deep .el-tree > .el-tree-node .el-tree-node__content {
      height: 56px;
      position: relative;
      .el-tree-node__expand-icon {
        position: absolute;
        right: 0;
        bottom: 14px;
        transform: rotate(-90deg);
        font-size: 18px;
      }
      .el-tree-node__label {
        font-size: 16px;
        margin-left: 15px;
      }
      .el-tree-node__expand-icon.expanded {
        transform: rotate(0deg);
      }

      .el-icon-caret-right:before {
        content: "\e6df";
      }
    }

    ::v-deep.el-menu {
      border: none;
    }
  }

  .rightContent {
    min-width: calc(100vh - 230px);
    width: 100%;
    background-color: #fff;
    margin-left: 15px;

    border-radius: 5px;
    box-sizing: border-box;

    .rightTop {
      width: 100%;
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 20px 40px 0;
      box-sizing: border-box;

      .rightTitle {
        color: #65a6e0;
        font-size: 20px;
        font-weight: bold;
      }

      .rightSearch {
        display: flex;

        .resetBtn {
          height: 38px;
          background-color: #6ccfcc;
          display: flex;
          align-items: center;
          border: none;
          span,
          i {
            color: #fff;
          }

          i {
            font-size: 16px;
          }
        }

        .rightSearchInput {
          height: 34px;
          display: flex;
          align-items: center;
          color: #d4d4d4;
          border: solid 1px #d4d4d4;
          margin-right: 15px;
          border-radius: 5px;
          line-height: 32px;

          ::v-deep .el-input__inner {
            height: 32px;
            border: none !important;
          }

          ::v-deep .el-input__icon {
            font-size: 18px;
            line-height: 34px;
          }

          ::v-deep .el-icon-search {
            font-size: 18px;
            line-height: 34px;
          }
        }

        ::v-deep .el-button {
          padding: 0 10px;
          height: 34px;
          line-height: 34px;
        }

        ::v-deep.el-icon-plus,
        ::v-deep.el-icon-close {
          font-size: 16px;
        }
      }
    }

    .rightBottom {
      margin-top: 20px;
      box-sizing: border-box;
      .bianji,.del {
        cursor: pointer;
        // font-size: 20px;
        font-weight: bold;
        color: #1990FF;
        margin: 0 6px;
      }

      .pagination {
        display: flex;
        align-items: center;
        justify-content: right;
        margin-top: 20px;
        margin-right: 20px;

        .jumper {
          font-size: 14px;
          margin-left: 20px;
          margin-right: 10px;
          display: flex;
          align-items: center;

          input {
            width: 40px;
            height: 26px;
            margin: 0 5px;
            text-align: center;
            border: solid 1px #e6e6e6;
            border-radius: 5px;
            color: #666;
            outline-style: none;
          }
        }

        ::v-deep .btn-prev,
        ::v-deep .btn-next,
        button {
          height: 28px;
          line-height: 28px;
          border-radius: 5px;
          font-size: 14px;
          padding: 0 8px;
          border: solid 1px #e6e6e6;
          color: #666;
        }

        .submit {
          color: #fff;
          background-color: #4895db;
          border: solid 1px #4895db;
        }
      }
    }
  }

  .dialog {
    ::v-deep .el-dialog {
      max-width: 750px;
      min-width: 500px;
      min-height: 426px;
      border-radius: 10px;
      background-color: #f9f9f9;
    }

    ::v-deep .el-dialog__header {
      height: 48px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 18px;
      background-color: #4895db;
      padding: 0;
      border-radius: 10px 10px 0 0;

      span {
        font-size: 16px;
        color: #fff;
      }
    }

    ::v-deep .el-dialog__headerbtn {
      top: 15px;
    }

    ::v-deep .el-dialog__headerbtn .el-dialog__close {
      color: #fff;
      font-size: 18px;
    }

    ::v-deep .el-dialog--center .el-dialog__body {
      text-align: initial;
      padding: 100px 120px;
      display: flex;
      justify-content: center;
    }

    ::v-deep .el-input__inner {
      height: 30px;
      line-height: 30px;
    }

    ::v-deep .el-form-item__content {
      width: 300px;
    }

    ::v-deep .el-dialog__footer {
      padding: 10px 20px 50px;
    }

    ::v-deep .el-button--default {
      width: 130px;
      border-radius: 20px;
      background-color: #f0f0f0;
      color: #aaaaaa;
      border: none;
    }

    ::v-deep .el-button + .el-button {
      width: 130px;
      border-radius: 20px;
      margin-left: 50px;
      background-color: #4895db;
      border-color: #4895db;
    }
  }
}
</style>